package com.wishu.finance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wishu.finance.config.AjaxResult;
import com.wishu.finance.domain.*;
import com.wishu.finance.enums.FinanceProductTypeEnum;
import com.wishu.finance.service.TJrProductService;
import com.wishu.finance.service.TJrSfLoanService;
import com.wishu.finance.service.TSfContractService;
import com.wishu.finance.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("contract")
public class ContractController {

    @Autowired
    TSfContractService tSfContractService;

    @Autowired
    TJrProductService tJrProductService;

    @Autowired
    TJrSfLoanService tJrSfLoanService;

    /**
     * 申请收房贷：收房合同查询
     *
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(Page page, TSfContract control) {
        return AjaxResult.success(tSfContractService.selectPageVo(page, control));
    }

    //根据ID查询合同信息
    @GetMapping("contractListById")
    public AjaxResult contractListById(Long id) {
        return AjaxResult.success(tSfContractService.contractListById(id));
    }

    //获取金融产品
    @GetMapping("queryJrProduct")
    public AjaxResult queryJrProduct() {
        return AjaxResult.success(tJrProductService.queryJrProduct(FinanceProductTypeEnum.SFLOAN.getIndex()));
    }

    @GetMapping("listSfLoan")
    public AjaxResult listSfLoan(@Valid SfLoanDetailRequestBean bean) {
        // 收房贷计算
        SfContractDetailsResponseDTO sfentity = tSfContractService.getSfContractStagingCalculation(bean.getContractId(), bean.getProductId());
        SearchSfLoanDetailRequestDto sfdto = new SearchSfLoanDetailRequestDto();
        BeanUtils.copy(bean, sfdto);

        sfdto.setBalance(Double.parseDouble(bean.getBalance()));    // 借款金额
        sfdto.setSxBalance(Double.parseDouble(bean.getSxBalance())); // 授信额
        sfdto.setPeriods(bean.getPeriods());    // 期数
        sfdto.setStartDate(bean.getStartDate()); // 合同开始日
        sfdto.setEndDate(bean.getEndDate());    // 合同结束日
        // 分期计算
        SearchSfLoanDetailsResponseDto sfr = tJrSfLoanService.getSfLoanDetail(sfdto);
        HashMap<String, Object> map = new HashMap<>();
        map.put("dataList", sfr);
        map.put("sfentity", sfentity);
        return AjaxResult.success(map);
    }
}
